<?php
/**
 * This file is part of DomMonitor.
 * 
 * DomMonitor is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * DomMonitor is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with DomMonitor.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @link http://inditel.ee/
 * @copyright 2007-2009 Inditel Meedia OÜ
 * @author Oliver Leisalu <oliver@inditel.ee>
 */

/**
 * This is a helper class for DomMonitor. It builds time tree from start and end times. See testTree() method for sample.
*
* @author Oliver Leisalu, Inditel Meedia OÜ
* @package DomMonitor
*/
class DomMonitorTimeTree {
	
	
	public static function build( array $times ) {
		$tree = &$times;

		$count = count($times);
		for( $i=0; $i<$count; $i++ ) {
			for( $s=0; $s<$count; $s++ ) {
				//debug($tree[$i]['start'].' >= '.$tree[$s]['start'].' && '.$tree[$i]['end'].' <= '.$tree[$s]['end']);
				if( $i != $s && $tree[$i]['start'] >= $tree[$s]['start'] && $tree[$i]['end'] <= $tree[$s]['end']) {
					
					// if parent exists, check that maybe current timeframe is smaller than parents. If it is, then parent is this.
					if( $tree[$i]['parent'] ) {
						$parentDif = $tree[$i]['parent']['end'] - $tree[$i]['parent']['start'];
						$currentDif = $tree[$s]['end'] - $tree[$s]['start'];
						if( $currentDif < $parentDif ) {
							unset( $tree[$i]['parent']['children'][$i] );
							$tree[$s]['children'][$i] = &$tree[$i];
							$tree[$i]['parent'] = &$tree[$s];
						}
					
					} else {
						$tree[$s]['children'][$i] = &$tree[$i];
						$tree[$i]['parent'] = &$tree[$s];
					}
				}
			}	
		}
		
		return $tree;
	}
	
	public static function calculate( array $times ) {
		
		$tree = self::build( $times );
		
		foreach( $tree as &$element ) {
			
			$element['realTotalTime'] = $element['end'] - $element['start'];
			$element['totalTime'] = $element['end'] - $element['start'];
			
			foreach( (array)$element['children'] as $child )
 				$element['subActionsTime'] += $child['end'] - $child['start'];

			$element['totalTime'] -= $element['subActionsTime'];
		}

		return $tree;
	}
	
	public static function testTree() {
		$asd = array(
			array(
				'start' => 1,
				'end' => 12,
				'info' => 'SECOND',
				'correct' => 6,
			),
			array(
				'start' => 2,
				'end' => 7,
				'info' => 'SECOND SUB',
				'correct' => 3,
			),
			array(
				'start' => 2,
				'end' => 4,
				'info' => 'SECOND SUB SUB',
				'correct' => 0,
			),
			array(
				'start' => 5,
				'end' => 6,
				'info' => 'SECOND SUB SUB',
				'correct' => 0,
			),
			array(
				'start' => 8,
				'end' => 9,
				'info' => 'SECOND SUB',
				'correct' => 0,
			),
			array(
				'start' => 0,
				'end' => 1,
				'info' => 'FIRST',
				'correct' => 0,
			),
			array(
				'start' => 15,
				'end' => 17,
				'info' => 'THIRD',
				'correct' => 0,
			),
		
		);
		
		debug( self::calculate($asd) );

	}
	
}
?>